import request from "../../utils/http";
import { anyObj, enter, promisify, showConfirm, wxToast } from "../../utils/util"

// pages/reservationFirst/reservationFirst.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		query: <anyObj>{},
		address: "",
		formData: {
			"contacts": "",
			"detailAddress": "",
			"lat": "",
			"lng": "",
			"outletId": 0,
			"phone": "",
			"street": "",
			"tag": "",
			"age": 0,
			"floorNum": 0,
			"gender": 0,
			"weight": 0,
			"stws": 0,
			"snszcd": [],
			"cgqk": [],
			"qtjcjb": [],
			"isLift": 0,
			"name": "",
			"waterHeaterType": 0
		},
		radioList: [
			{
				key: 'weight',
				label: '体重',
				options: [
					{
						value: 0,
						label: '小于50KG'
					},
					{
						value: 1,
						label: '50-75KG'
					},
					{
						value: 2,
						label: '75-90KG'
					},
					{
						value: 3,
						label: '90KG以上'
					}
				]
			},
			{
				key: 'stws',
				label: '身体外伤',
				options: [
					{
						label: '无',
						value: 0
					},
					{
						label: '轻度',
						value: 1
					},
					{
						label: '中度',
						value: 2
					},
					{
						label: '其他',
						value: 3
					},
				]
			},
			{
				key: 'isLift',
				label: '是否电梯',
				options: [
					{
						label: '电梯',
						value: '1'
					},
					{
						label: '楼梯',
						value: 0
					},
				]
			},
			{
				key: 'waterHeaterType',
				label: '热水器类型',
				options: [
					{
						label: '电热水器',
						value: 0
					},
					{
						label: '燃气热水器',
						value: 1
					},
				]
			}
		],
		checkboxList: [
			{
				key: 'snszcd',
				label: '失能失智程度',
				options: [
					{
						label: '全失能',
						value: 0
					},
					{
						label: '半失能',
						value: 1
					},
					{
						label: '搀扶下可行走',
						value: 2
					},
					{
						label: '其他',
						value: 3
					}
				]
			},
			{
				key: 'cgqk',
				label: '插管情况',
				options: [
					{
						label: '无',
						value: 0
					},
					{
						label: '胃管',
						value: 1
					},
					{
						label: '尿管',
						value: 2
					},
					{
						label: '其他',
						value: 3
					},
				]
			},
			{
				key: 'qtjcjb',
				label: '其他基础疾病',
				options: [
					{
						label: '无',
						value: 0
					},
					{
						label: '脑梗',
						value: 1
					},
					{
						label: '心梗',
						value: 2
					},
					{
						label: '高血压',
						value: 3
					},
					{
						label: '糖尿病',
						value: 4
					},
					{
						label: '其他',
						value: 5
					},
				]
			}
		]
	},

	/**
	 * 选择地图
	 */
	async chooseLocation() {
		try {
			const data = await promisify(wx.chooseLocation);
			const formData = this.data.formData;
			const addressData = await request({
				url: '/api/customers/reservation/getOutlet',
				method: 'POST',
				data: {
					"lat": data.latitude,
					"lng": data.longitude,
				},
				errToast: true,
				showLoading: {title: '正在获取地址数据...'},
				contentTypeForm: true
			});
			if (!addressData) {
				wxToast({icon: 'error', title: '当前暂无网点'});
			}
			formData.lat = data.latitude;
			formData.lng = data.longitude;
			formData.outletId = addressData.id;
			// formData.street = addressData.street;
			this.setData({
				formData,
				address: data.address
			});
		} catch (error) {
			
		}
	},

	async onChangeForm(e: WechatMiniprogram.CustomEvent) {
		console.log("e: ", e)
		const formData = this.data.formData as any;
		formData[e.currentTarget.dataset.key] = e.detail.value;
		console.log("formData: ", formData)
		this.setData({
			formData
		});
	},

	async submit() {
		const formData = this.data.formData;

		if (!this.data.address) {
			return wxToast({icon: 'error', title: '请选择地址'});
		}
		if (!formData.detailAddress) {
			return wxToast({icon: 'error', title: '请输入详细地址'});
		}
		if (!formData.contacts) {
			return wxToast({icon: 'error', title: '请输入联系人'});
		}
		if (!formData.phone) {
			return wxToast({icon: 'error', title: '请输入联系电话'});
		}
		if (!formData.age) {
			return wxToast({icon: 'error', title: '请输入年龄'});
		}
		if (!formData.tag) {
			return wxToast({icon: 'error', title: '请输入地址标签'});
		}
		if (!formData.floorNum) {
			return wxToast({icon: 'error', title: '请输入楼层'});
		}
		if (!formData.name) {
			return wxToast({icon: 'error', title: '请输入被服务人姓名'});
		}
		await showConfirm({content: '确定提交?'});
		try {
			const healthInfo = [`TZ:${formData.weight}`, `STWS:${formData.stws}`];
			if (formData.snszcd.length > 0) {
				healthInfo.push(`SNSZCD:${formData.snszcd.join(",")}`);
			}
			if (formData.cgqk.length > 0) {
				healthInfo.push(`CGQK:${formData.cgqk.join(",")}`);
			}
			if (formData.qtjcjb.length > 0) {
				healthInfo.push(`QTJCJB:${formData.qtjcjb.join(",")}`);
			}
			await request({
				url: '/api/customers/reservation/saveAddressAndBodyInfo',
				method: 'POST',
				contentTypeForm: false,
				errToast: true,
				showLoading: {title: '正在提交...'},
				data: {
					addressDto: {
						"outletId": formData.outletId,
						"id": 0,
						"contacts": formData.contacts,
						"detailAddress": formData.detailAddress,
						"isDefault": 1,
						"lat": formData.lat,
						"lng": formData.lng,
						"phone": formData.phone,
						"tag": formData.tag
					},
					"familyMemberDto": {
						"age": formData.age,
						"floorNum": formData.floorNum,
						"gender": formData.gender,
						"healthInfo": healthInfo.join(";"),
						"id": 0,
						"isDefault": 1,
						"isLift": formData.isLift,
						"name": formData.name,
						"waterHeaterType": formData.waterHeaterType
					},
					orderId: this.data.query.orderId
				}
			});
			wxToast({icon: 'success', title: '提交成功'});
			enter({
				url: `/pages/chooseScheduleDate/chooseScheduleDate`,
				query: {
					outletId: formData.outletId,
					orderId: this.data.query.orderId,
					serviceId: this.data.query.serviceId
				}
			});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query = {}) {
		this.setData({
			query
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})